#-------------------------------------------------------------------------------
# Student Name: Joe Student / Assignment: PA #5 / Date: 09/09/9999
#-------------------------------------------------------------------------------
# Honor Code Statement: I received no assistance on this assignment that 
# violates the ethical guidelines set forth by professor and class syllabus.
#-------------------------------------------------------------------------------
# References: (list of web sites, texts, and any other resources used) 
#-------------------------------------------------------------------------------
# Comments: (a note to the grader as to any problems or uncompleted aspects of 
#            of the assignment) 
#-------------------------------------------------------------------------------
# Pseudocode: N/A (not applicable; not required)
#-------------------------------------------------------------------------------
# Assumptions: Any interpretation of the spec should be placed here.
#-------------------------------------------------------------------------------
# NOTE: width of source code should be <= 80 characters to facilitate printing 
#-------------------------------------------------------------------------------

import csv
# Might not need
import os

# Might need to be lists and not tuples
a1 = ("One", "one", "1", 1)
a2 = ("Two", "two", "2", 2)
a3 = ("Three", "three", "3", 3)
a4 = ("Four", "four", "4", 4)
a5 = ("Five", "five", "5", 5)
a6 = ("Six", "six", "6", 6)
a7 = ("Seven", "seven", "7", 7)
a8 = ("Eight", "eight", "8", 8)
a9 = ("Nine", "nine", "9", 9)
choices = (a1, a2, a3, a4, a5, a6, a7, a8)

def main_menu(dictionary=None):
    """Present main menu"""
    choice = raw_input("Menu\n\n1) Open file\n2) Start a new phone " + 
                       "numbers database\n3) Add a phone number\n4) Remove " + 
                       "a phone number\n5) Look up a person's phone number\n" + 
                       "6) Reverse-lookup a name by number\n7) Save the " + 
                       "updated file\n8) Display entire phone directory " + 
                       "alphabetically\n9) Exit\n\nPlease make a" + 
                       " selection: ")
    if (i for i, choice in choices):
        if choice in a1:
            return
        elif choice in a2:
            dictionary = new_phonebook()
        elif choice in a3:
            dictionary = add_entry(dictionary)
        elif choice in a4:
            dictionary = del_entry(dictionary)
        elif choice in a5:
            print find_number(dictionary)
        elif choice in a6:
            print find_name(dictionary)
        elif choice in a7:
            return
        elif choice in a8:
            return
        elif choice in a9:
            exit()
        main_menu(dictionary)
    else:
        print "Please enter a valid selection"
        main_menu()

# NOT IMPLEMENTED YET!!!        
def open_file():
    print_dir()
    fileName = str(raw_input("\nEnter the file path (.txt): "))
    try:
        os.path.exists(fileName)
    except:
        print "That is not a valid entry."
        open_file()  
    f = open(fileName, "r+")
    read_dict = csv.DictReader(f, [])
    
    f.close()
    
def new_phonebook():
    dictionary = {}
    print "Ready"
    return dictionary

def add_entry(dictionary):
    name = str(raw_input("Enter name: "))
    number = str(raw_input("Enter number: "))
    dictionary[name] = number
    return dictionary  

def del_entry(dictionary):
    name = str(raw_input("Enter name: "))
    if name in dictionary:
        choice = str(raw_input("Are you sure you want to delete " + name + 
                               "?\nYes or No: "))
        if choice == "Yes" or choice == "yes":
            del dictionary[name]
    else:
        print "That record doesn't exist or you chose not to delete it."
    main_menu(dictionary)
  
def find_number(dictionary):
    name = str(raw_input("Enter name: "))
    if name in dictionary:
        return str(dictionary[name])
    else:
        return "That record doesn't exist."
    
def find_name(dictionary):
    number = str(raw_input("Enter number: "))
    try:
        return str([k for k, v in dictionary.iteritems() if v == number][0])
    except:
        return "That record doesn't exist."
    
def print_dir():
    """Presents the current directory in a readable format"""
    print ("Current directory:\n")
    for dirname, dirnames, filenames in os.walk('.'):
        for subdirname in dirnames:
            print "\t" + os.path.join(dirname, subdirname)
        for filename in filenames:
            print "\t" + os.path.join(dirname, filename) 

def main():
    main_menu()

main()
